<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<jsp:include page="head.jsp"/>
<%--中间内容开始--%>
<div class="layui-container fly-marginTop fly-user-main">
    <jsp:include page="manger_left.jsp"/>

    <div class="fly-panel fly-panel-user" pad20>

        <div class="layui-tab layui-tab-brief" lay-filter="user">
            <ul class="layui-tab-title" id="LAY_mine">
                <li data-type="mine-jie" lay-id="index"><a href="${pageContext.request.contextPath}/fileInfo/student/to_list.do">我的文件</a></li>
                <li data-type="mine-jie" lay-id="index" class="layui-this">上传文件</li>
            </ul>
            <div class="layui-tab-content" style="padding: 20px 0;">

                <!-- 上传文件信息 -->
                <div class="layui-tab-item layui-show">
                    <div class="layui-form layui-form-pane">
                        <form:form id="fm" class="layui-form" action="" enctype="multipart/form-data" method="post">
                        <%--<form:form action="${pageContext.request.contextPath}/fileInfo/student/add.do" enctype="multipart/form-data" method="post">--%>
                            <div class="layui-form-item">
                                <label for="filename" class="layui-form-label">文件标题</label>
                                <div class="layui-input-inline">
                                    <input type="text" id="filename" name="fName" required lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label for="fState" class="layui-form-label">文件说明</label>
                                <div class="layui-input-block">
                                    <textarea placeholder="请输入内容" id="fState" name="fState" class="layui-textarea"></textarea>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label for="uploadFile" class="layui-form-label">选择文件</label>
                                <div class="layui-input-inline">
                                    <input id="uploadFile" type="file" name="uploadFile"/>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <button class="layui-btn"  lay-filter="addForm" lay-submit>立即上传</button>
                            </div>
                        </form:form>
                    </div>
                    <div id="LAY_page1"></div>
                </div>
            </div>
        </div>
    </div>
</div>
<%--中间内容结束--%>
<jsp:include page="bottom.jsp"/>

<%--表格渲染--%>
<script>
    layui.use('table', function(){
        var table = layui.table;

        table.render({
            elem: '#test'
            ,url:'${pageContext.request.contextPath}/fileInfo/student/list.do'
            ,toolbar: 'default' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            , id: 'test'
            ,title: '文件数据表'
            ,cols: [[
                { fixed: 'left',type: 'checkbox',width:'5%'}
                ,{field:'fId', title:'ID',type:"numbers", width:'5%', unresize: true, align:'center'}
                ,{field:'fName', title:'文件名', width:'12%', sort: true, align:'center',edit:'true'}
                ,{field:'fSize', title:'文件大小', width:'12%', sort: true, align:'center'}
                ,{field:'fCreateTime', title:'上传时间', width:'18%', sort: true, align:'center'}
                ,{field:'fType', title:'类型', width:'10%', sort: true, align:'center'}
                ,{field:'user', title:'上传用户', width:'12%', sort: true, align:'center',templet: '<div>{{d.user.userName}}</div>'}
                ,{field:'fState', title:'备注', width:'12%',sort: true, align:'center',edit:'true'}
                ,{title:'操作', toolbar: '#barDemo', width:'14%', align:'center'}
            ]]
            ,page: true,
            text: {
                none: '暂无相关数据'
            }
        });
        //数据表格重载
        // 执行搜索，表格重载
        $('#search').on('click', function () {
            // 搜索条件
            var demoReload = $('#demoReload').val();
            var startTime = $('#startTime').val();
            var endTime = $('#endTime').val();
            table.reload('test', {
                method: 'get'
                , where: {
                    'keywords': demoReload
                    ,'startTime':startTime
                    ,'endTime':endTime
                }
                , page: {
                    curr: 1
                }
            });
        });

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //监听头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event){
                case 'add':
                    window.location.href="${pageContext.request.contextPath}/blog/to_add.do";
                    break;
                case 'update':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else if(data.length > 1){
                        layer.msg('只能同时编辑一个');
                    } else {
                        window.location.href="${pageContext.request.contextPath}/fileInfo/student/to_edit.do?fId="+data[0].fId;
                    }
                    break;
                case 'delete':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else {
                        var ids = [];//id
                        var fName = [];//name
                        for (var i = 0; i < data.length; i++) { //循环筛选出id
                            ids.push(data[i].fId);
                            fName.push(data[i].fName);
                        }
                        //发送删除请求
                        layer.confirm("确认要删除"+JSON.stringify(fName)+"吗，删除后不能恢复", { title: "删除确认" }, function(index){
                            var ids = data.fId;
                            senddel(ids);
                            return false;
                        });


                    }
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(data.uId);
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    var ids = data.fId;
                    senddel(ids);
                    return false;
                });
            }else if(obj.event === 'detail'){
                window.location.href="${pageContext.request.contextPath}/fileInfo/to_fileInfo.do?fId="+data.fId;
            }
        });
        /*删除请求*/
        function senddel(ids){
            $.ajax({
                url: "${pageContext.request.contextPath}/fileInfo/teacher/admin_delete.do",
                type: "post",
                data: 'ids='+ids,
                success: function (result) {
                    if (result.msg == 200) {
                        //关闭弹框
                        //layer.close(index);
                        layer.msg("删除成功", {icon: 6});
                        table.reload('test',{});
                    } else {
                        layer.msg("删除失败", {icon: 5});
                    }
                }
            });
        };

        //监听单元格编辑
        table.on('edit(test)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            table_edit_update(data.fId,field,value);
        });

    });
    /*选择时间*/
    layui.use('laydate', function() {
        var laydate = layui.laydate;
        laydate.render({
            elem: '#startTime'
            , theme: 'molv'
        });
        laydate.render({
            elem: '#endTime'
            , theme: 'molv'
        });
    });

    //编辑单元格发送请求
    function table_edit_update(fId,field,value){
        $.post('${pageContext.request.contextPath}/fileInfo/teacher/admin_update.do?'+field+'='+value,{fId:fId},function(result){
            if(result.code==0){
                layer.msg(result.msg);
            }else{
                layer.closeAll();
                layer.msg(result.msg);
            }
        },'json');

    }
</script>
<%--提交表单--%>
<script>
    layui.use(['layer','form'],function(){
        var form=layui.form;
        var layer=layui.layer;
        form.on('submit(addForm)', function(data){
            var saveLoading = layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
            var formData = new FormData($( "#fm" )[0]);
            $.ajax({
                url:'${pageContext.request.contextPath}/fileInfo/student/add.do',
                method:'post',
                async: false,
                data : formData,
                cache:false,
                contentType: false,
                processData: false,
                success:function(res){
                    layer.close(saveLoading);
                    switch(res.code){
                        case 200:
                            layer.msg(res.msg, {icon: 1});
                            layui.table.reload('test');//重载父页表格，参数为表格ID
                            layer.close(index);
                            break;
                        case -1:
                            parent.layer.msg(res.msg, {icon: 2});
                            break;
                        case -2:
                            //登录失效
                            alert('登录失效');
                            window.open("${pageContext.request.contextPath}/user/to_login.do");
                            break;
                    }
                },
                error:function (data) {

                }
            }) ;

        });
    });
</script>